package beer.coder.walawala.gui.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * TODO
 *
 * @author amath0312
 * @version 1.0
 * @date 2019-12-14 22:30
 */
public class DateUtil {

  public static final String DEFAULT_PATTERN = "yyyyMMddHHmmss";

  public static String now() {
    return format(System.currentTimeMillis());
  }

  public static String format(long timestamp) {
    SimpleDateFormat format = new SimpleDateFormat(DEFAULT_PATTERN);
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(timestamp);
    return format.format(cal.getTime());
  }

  public static Calendar parse(String date) {
    SimpleDateFormat format = new SimpleDateFormat(DEFAULT_PATTERN);
    try {
      Date d = format.parse(date);
      Calendar cal = Calendar.getInstance();
      cal.setTime(d);
      return cal;
    } catch (ParseException e) {
      throw new IllegalArgumentException("wrong date format: " + date, e);
    }
  }
}
